<?php

namespace Admin\Controller;
use Think\Controller;

// 商品控制器
class GoodsController extends \Components\AdminController {

	// 商品列表
	public function showlist(){
		$goodsModel = M('goods');
		$recoredCount = $goodsModel->count();//总记录数
		$page = new \Think\Page($recoredCount, 5);
		
		// 分页配置
		$page->lastSuffix = false;
		$page->rollPage = 10;
		$page->setConfig('prev', '【上一页】');
		$page->setConfig('next', '【下一页】');
		$page->setConfig('first', '【首页】');
		$page->setConfig('last', '【末页】');
		$page->setConfig('theme', '<span class="rows">共 %TOTAL_ROW% 条记录</span> %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

		$list = $goodsModel->limit("$page->firstRow, $page->listRows")->select();

		$pagestr = $page->show();// 组装分页字符串

		$this->assign('list', $list);
		$this->assign('pagestr', $pagestr);
		$this->display();
	}

	// 添加商品
	public function add(){
		// 获取所有分类
		$cats = M('category')->select();
		$this->assign('cats', $cats);

		if(IS_POST) {
			// 执行添加操作

			// 方法一
			// $data['goods_name'] = $_POST['goods_name'];
			// $data['goods_category_id'] = $_POST['goods_category_id'];
			// $data['goods_price'] = $_POST['goods_price'];
			// $data['goods_introduce'] = $_POST['goods_introduce'];

			// $msg = '添加失败';
			// if(M('goods')->add($data)){
			// 	$msg = '添加成功';
			// }
			// $this->redirect('showlist', array(), 3, $msg);

			// 方法二，使用create自动匹配post字段，并创建对象
			$goods = M('goods');
			;// create()方法如果没有传值默认取POST数据
			if ($data = $goods->create()) {

				// 处理图片上传
				if ($_FILES['goods_img']['error'] == 0) {
					$config = array('rootPath' => './Application/public/uploads/');
				}
				$upload = new \Think\Upload($config);
				$info = $upload->uploadOne($_FILES['goods_img']);
				var_dump($info);
				// 大图保存路径
				$data['goods_big_img'] = $info['savepath'] . $info['savename'];
				

				// 生成缩略图
				$img = new \Think\Image();
				// 1、打开图片
				$big_img = $upload->rootPath . $data['goods_big_img'];//大图的绝对路径
				$img->open($big_img);

				// 2、生成缩略图
				$img->thumb(200, 300, 2);

				// 3、保存缩略图
				$small_img = $upload->rootPath . $info['savepath'] . 'small_'. $info['savename'];
				$img->save($small_img);
				$data['goods_small_img'] = $info['savepath'] . 'small_'. $info['savename'];
				
				$data['goods_create_time'] = time();// 创建时间

				if ($goods->add($data)) {
					$this->success('添加成功', U('showlist'), 3);
				} else {
					$this->error('添加失败');
				}
			}

		}

		$this->display();
	}

	// 修改商品
	public function update($goods_id = ''){
		if (IS_POST) {
			$goods = M('goods');
			$data = $goods->create();
			$data['goods_create_time'] = time();
			if ($goods->save($data)) {
				$this->success('修改成功', U('showlist'), 3);
			} else {
				$this->error('修改失败');
			}
		}

		// 获取所有分类
		$cats = M('category')->select();
		$this->assign('cats', $cats);

		// 获取对应的商品实体
		$data = M('goods')->find($goods_id);
		$this->assign('data', $data);

		$this->display();
	}

	public function send(){
		$obj = new \Components\EmailTool();
		$obj->send();
	}

	// 删除商品
	public function delete($goods_id = ''){
		if (M('goods')->delete($goods_id)) {
			$this->success('删除成功', U('showlist'), 3);
		} else {
			$this->error('删除失败');
		}
	}

	public function test1(){
		$goodsModel = M('goods');
		$data = $goodsModel->getBygoods_id(2);
		echo $goodsModel->max('goods_price') . '<br>';
		echo $goodsModel->min('goods_price') . '<br>';
		echo $goodsModel->avg('goods_price') . '<br>';
		echo $goodsModel->sum('goods_price') . '<br>';
		echo $goodsModel->count() . '<br>';
		// echo '<pre>';
		// var_dump($data);
	}

	public function test3(){
		$data = array(
			'goods_name' => '手机', 
			'goods_price' => 100,
			'abc' => 1,
			);
		$result = M('goods')->add($data);
		echo $result;
	}

	public function test4(){
		$goods = M('goods');
		$goods->goods_name = '电视机';
		$goods->goods_price = 142;
		$goods->abc = 1;
		echo $goods->add($data);
	}

	public function update1(){
		$goods = M('goods');
		$goods->goods_id = 140;
		$goods->goods_name = '电视机2';
		$goods->goods_price = 143;
		$goods->abc = 1;
		echo $goods->save();
	}

	public function update2(){
		$goods = M('goods');
		$data = array('goods_id' => 140, 'goods_name' => '电视机3', 'abc' => 2);
		echo $goods->save($data);
	}

	public function delete1(){
		echo M('goods')->where("goods_name = '手机'")->delete();
	}

	public function sql(){
		$data = M('goods')->query('select * from sw_goods where goods_id = 2 ');
		var_dump($data);
	}
}